﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace WPlayer
{
    public partial class Form3 : Form
    {
        byte rate;
        bool cancel;
        Thread thread;

        public Form3() => InitializeComponent();

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            var fs = e.Data.GetData("FileDrop") as string[];
            if (fs == null || fs.Length == 0 || Program.IsInvalidFile(fs[0])) return;
            e.Effect = DragDropEffects.Link;
        }

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            var fs = e.Data.GetData("FileDrop") as string[];
            if (fs == null || fs.Length == 0) return;
            this.textBox1.Text = fs[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog() { Title = "打开媒体文件", Filter = "媒体文件|*.*", CheckFileExists = true })
            {
                if (dlg.ShowDialog() != DialogResult.OK) return;
                if (Program.IsInvalidFile(dlg.FileName)) return;
                this.textBox1.Text = dlg.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (var dlg = new SaveFileDialog() { Title = "选择保存路径", Filter = "加密媒体|*.mda|自定义|*.*", CheckFileExists = false, CheckPathExists = true })
            {
                dlg.FileName = System.IO.Path.GetFileNameWithoutExtension(this.textBox1.Text);
                if (dlg.ShowDialog() != DialogResult.OK) return;
                if (Program.IsInvalidFile(dlg.FileName)) return;
                this.textBox2.Text = dlg.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.textBox1.TextLength == 0)
            {
                MessageBox.Show("请先选择要加密的媒体文件！");
                return;
            }
            if (this.textBox2.TextLength == 0)
            {
                MessageBox.Show("必须选择保存路径！");
                return;
            }
            this.rate = 0;
            this.textBox3.Text = "0";
            if (System.IO.File.Exists(this.textBox2.Text)) System.IO.File.Delete(this.textBox2.Text);
            this.panel1.Enabled = false;
            (this.thread = new Thread(() =>
            {
                WangfsLib.Interface.VlcPlayer.VlcMediaPlayer.EncodeMediaFile(this.textBox1.Text, this.textBox2.Text, this.setProgress, this.textBox4.Text);
                WangfsLib.Interface.VlcPlayer.VlcMediaPlayer.SetProgressValue(0);
                if (this.cancel) return;
                this.Invoke((Action)(() => this.panel1.Enabled = true));
            })).Start();
        }

        bool setProgress(string file, byte rate)
        {
            if (this.rate != rate)
            {
                WangfsLib.Interface.VlcPlayer.VlcMediaPlayer.SetProgressValue(rate);
                this.Invoke((Action)(() => this.textBox3.Text = (this.rate = rate).ToString()));
            }
            return this.cancel;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (this.textBox3.TextLength > 0 && this.rate < 100)
            {
                if (e.Cancel = MessageBox.Show(this, "正在加密媒体文件，退出将中止加密并删除未完成的文件，是否退出？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
                this.cancel = true;
            }
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            if (this.thread == null) return;
            if (this.thread.IsAlive) this.thread.Join();
        }
    }
}
